package arsmodeller.editpolicies;

import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.XYLayoutEditPolicy;
import org.eclipse.gef.requests.CreateRequest;

import arsmodeller.commands.ThingChangeConstraintCommand;
import arsmodeller.commands.ThingCreateCommand;
import arsmodeller.model.ObjectProcessingDiagram;
import arsmodeller.model.Thing;

/**
 * This class describes the commands that can be used to change the layout
 * inside the ObjectProcessDiagram, Create new entities inside the
 * ObjectProcessDiagram
 * 
 * @author vainolo
 * 
 */
public class ObjectProcessDiagramXYLayoutPolicy extends XYLayoutEditPolicy {

	/**
	 * Command created the user requests to change the constraint (size,
	 * location) of an object that is part of an OPD.
	 */
	@Override
	protected Command createChangeConstraintCommand(EditPart child,
			Object constraint) {
		ThingChangeConstraintCommand command = new ThingChangeConstraintCommand();
		command.setModel((Thing) child.getModel());
		command.setNewConstraint((Rectangle) constraint);
		return command;
	}

	/**
	 * Command created to add new things to the OPD.
	 */
	@Override
	protected Command getCreateCommand(CreateRequest request) {
		Command retVal = null;
		if (request.getNewObjectType().equals(Object.class)
				|| request.getNewObjectType().equals(Process.class)) {
			ThingCreateCommand command = new ThingCreateCommand();
			command.setLocation(request.getLocation());
			command.setParent((ObjectProcessingDiagram) (getHost().getModel()));
			command.setThing((Thing) (request.getNewObject()));
			retVal = command;
		}
		return retVal;
	}
}
